package cn.iocoder.yudao.module.system.service.partnerstore;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.api.partnerstore.dto.PartnerStoreReqDTO;
import cn.iocoder.yudao.module.system.api.partnerstore.dto.PartnerStoreRespDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.partner.PartnerDO;
import cn.iocoder.yudao.module.system.dal.mysql.partner.PartnerMapper;
import cn.iocoder.yudao.module.system.enums.partner.PartnerTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.system.controller.admin.partnerstore.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.partnerstore.PartnerStoreDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.partnerstore.PartnerStoreMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 合伙人管理店铺 Service 实现类
 *
 * @author 万民匠心
 */
@Service
@Validated
public class PartnerStoreServiceImpl implements PartnerStoreService {

    @Resource
    private PartnerStoreMapper partnerStoreMapper;

    @Resource
    private PartnerMapper partnerMapper;

    @Override
    public Long createPartnerStore(PartnerStoreSaveReqVO createReqVO) {
        // 插入
        PartnerStoreDO partnerStore = BeanUtils.toBean(createReqVO, PartnerStoreDO.class);
        partnerStoreMapper.insert(partnerStore);
        // 返回
        return partnerStore.getId();
    }

    @Override
    public void updatePartnerStore(PartnerStoreSaveReqVO updateReqVO) {
        // 校验存在
        validatePartnerStoreExists(updateReqVO.getId());
        // 更新
        PartnerStoreDO updateObj = BeanUtils.toBean(updateReqVO, PartnerStoreDO.class);
        partnerStoreMapper.updateById(updateObj);
    }

    @Override
    public void deletePartnerStore(Long id) {
        // 校验存在
        validatePartnerStoreExists(id);
        // 删除
        partnerStoreMapper.deleteById(id);
    }

    private void validatePartnerStoreExists(Long id) {
        if (partnerStoreMapper.selectById(id) == null) {
            throw exception(PARTNER_STORE_NOT_EXISTS);
        }
    }

    @Override
    public PartnerStoreDO getPartnerStore(Long id) {
        return partnerStoreMapper.selectById(id);
    }

    @Override
    public PageResult<PartnerStoreDO> getPartnerStorePage(PartnerStorePageReqVO pageReqVO) {
        return partnerStoreMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PartnerStoreRespDTO> getPartnerStoreList(PartnerStoreReqDTO reqDTO) {
        List<PartnerDO> partnerList = partnerMapper.selectList(new LambdaQueryWrapperX<PartnerDO>()
                .eqIfPresent(PartnerDO::getStatus, 1)// 已启用
                .eq(PartnerDO::getDeleted, Boolean.FALSE));
        if (CollectionUtils.isNotEmpty(partnerList)) {
            List<Long> partnerIds = partnerList.stream().map(PartnerDO::getId).collect(Collectors.toList());
            List<PartnerStoreDO> dataList = Lists.newArrayList();
            List<PartnerStoreDO> initiatorStoreList = partnerStoreMapper.selectList(new LambdaQueryWrapperX<PartnerStoreDO>()
                    .eqIfPresent(PartnerStoreDO::getPartnerType, reqDTO.getPartnerType())
                    .inIfPresent(PartnerStoreDO::getPartnerId, partnerIds)
                    .le(PartnerStoreDO::getShareProfitStartDate, reqDTO.getShareProfitDate())
                    .ge(PartnerStoreDO::getShareProfitEndDate, reqDTO.getShareProfitDate())
                    .eq(PartnerStoreDO::getDeleted, Boolean.FALSE));

            if (CollectionUtils.isNotEmpty(initiatorStoreList)) {
                dataList.addAll(initiatorStoreList);
            }

//            List<PartnerStoreDO> partnerStoreList = partnerStoreMapper.selectList(new LambdaQueryWrapperX<PartnerStoreDO>()
//                    .inIfPresent(PartnerStoreDO::getReferrerType, partnerIds)
//                    .le(PartnerStoreDO::getShareProfitStartDate, reqDTO.getShareProfitDate())
//                    .ge(PartnerStoreDO::getShareProfitEndDate, reqDTO.getShareProfitDate())
//                    .eq(PartnerStoreDO::getDeleted, Boolean.FALSE));
//            if (CollectionUtils.isNotEmpty(partnerStoreList)) {
//                partnerStoreList.forEach(item -> {
//                    item.setPartnerType(PartnerTypeEnum.ADMIN.getType());
//                });
//                dataList.addAll(partnerStoreList);
//            }

            return BeanUtils.toBean(dataList, PartnerStoreRespDTO.class);
        }
        return Collections.emptyList();
    }

}